<?php
session_start();
require_once 'config/db.php';

$id = $_GET['id'] ?? 0;
if (!$id) {
    header("Location: index.php");
    exit;
}

try {
    $stmt = $pdo->prepare("SELECT * FROM articles WHERE id = ?");
    $stmt->execute([$id]);
    $article = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$article) {
        echo "<script>alert('文章不存在'); window.location.href='index.php';</script>";
        exit;
    }
} catch (PDOException $e) {
    echo "<script>alert('文章加载失败: " . $e->getMessage() . "'); window.location.href='index.php';</script>";
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $article['title']; ?> - IT运维知识库</title>
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar">
    <div class="container navbar-container">
        <div class="logo">
            <i class="fa fa-server"></i>
            <a href="index.php">IT运维知识库</a>
        </div>
        
        <ul class="nav-links">
            <li><a href="index.php"><i class="fa fa-home"></i> 首页</a></li>
            <li><a href="categories.php"><i class="fa fa-folder"></i> 分类</a></li>
            <li><a href="versions.php"><i class="fa fa-history"></i> 版本记录</a></li>
            
            <?php if (isset($_SESSION['user_id'])): ?>
                <li><a href="publish.php"><i class="fa fa-pencil"></i> 发布文章</a></li>
                <?php if ($_SESSION['role'] === 'admin'): ?>
                    <li><a href="admin/index.php"><i class="fa fa-cog"></i> 后台管理</a></li>
                <?php endif; ?>
                <li class="user-menu">
                    <a href="javascript:;">
                        <i class="fa fa-user"></i> <?php echo $_SESSION['username']; ?>
                    </a>
                </li>
            <?php else: ?>
                <li><a href="login.php" class="btn btn-primary"><i class="fa fa-sign-in"></i> 登录</a></li>
            <?php endif; ?>
        </ul>
        
        <button class="menu-toggle" id="menuToggle">
            <i class="fa fa-bars"></i>
        </button>
    </div>
</nav>

<!-- 移动端菜单 -->
<div class="mobile-menu" id="mobileMenu">
    <button class="close-btn" id="closeMenu"><i class="fa fa-times"></i></button>
    <ul class="mobile-menu-links">
        <li><a href="index.php"><i class="fa fa-home"></i> 首页</a></li>
        <li><a href="categories.php"><i class="fa fa-folder"></i> 分类</a></li>
        <li><a href="versions.php"><i class="fa fa-history"></i> 版本记录</a></li>
        
        <?php if (isset($_SESSION['user_id'])): ?>
            <li><a href="publish.php"><i class="fa fa-pencil"></i> 发布文章</a></li>
            <?php if ($_SESSION['role'] === 'admin'): ?>
                <li><a href="admin/index.php"><i class="fa fa-cog"></i> 后台管理</a></li>
            <?php endif; ?>
            <li><a href="javascript:;"><i class="fa fa-user"></i> <?php echo $_SESSION['username']; ?></a></li>
            <li><a href="reset_password.php"><i class="fa fa-key"></i> 重置密码</a></li>
            <li><a href="javascript:logout()"><i class="fa fa-sign-out"></i> 退出登录</a></li>
        <?php else: ?>
            <li><a href="login.php" class="btn btn-primary"><i class="fa fa-sign-in"></i> 登录</a></li>
            <li><a href="register.php" class="btn btn-secondary"><i class="fa fa-user-plus"></i> 注册</a></li>
        <?php endif; ?>
    </ul>
</div>

<!-- 主内容区 -->
<div class="main-content">
    <div class="container">
        <div class="card">
            <div class="card-body">
                <div class="article-detail-header">
                    <h1 class="article-detail-title">
                        <?php echo $article['title']; ?>
                        <?php if ($article['is_top']): ?><span class="badge badge-top">置顶</span><?php endif; ?>
                        <?php if ($article['is_essence']): ?><span class="badge badge-essence">精华</span><?php endif; ?>
                        <?php if ($article['is_recommend']): ?><span class="badge badge-recommend">推荐</span><?php endif; ?>
                    </h1>
                    
                    <div class="article-meta">
                        <span><i class="fa fa-folder"></i> <?php echo $article['category']; ?></span>
                        <span><i class="fa fa-user"></i> <?php echo $article['author']; ?></span>
                        <span><i class="fa fa-clock-o"></i> 
                            <?php echo $article['updated_at'] ? '更新于 ' . $article['updated_at'] : '发布于 ' . $article['created_at']; ?>
                        </span>
                    </div>
                </div>
                
                <div class="article-detail-content">
                    <?php echo nl2br($article['content']); ?>
                </div>
                
                <div style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee;">
                    <div style="display: flex; justify-content: space-between;">
                        <a href="index.php" class="btn btn-secondary"><i class="fa fa-arrow-left"></i> 返回列表</a>
                        
                        <?php if (isset($_SESSION['user_id']) && ($_SESSION['role'] === 'admin' || $_SESSION['username'] === $article['author'])): ?>
                            <a href="admin/articles.php?edit=<?php echo $article['id']; ?>" class="btn btn-primary">
                                <i class="fa fa-edit"></i> 编辑文章
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 页脚 -->
<footer class="footer">
    <div class="container">
        <div class="footer-content">
            <div class="footer-column">
                <h3><i class="fa fa-server"></i> IT运维知识库</h3>
                <p>分享IT运维经验，助力技术成长</p>
            </div>
            <div class="footer-column">
                <h3>快速链接</h3>
                <ul class="footer-links">
                    <li><a href="index.php"><i class="fa fa-home"></i> 首页</a></li>
                    <li><a href="categories.php"><i class="fa fa-folder"></i> 分类</a></li>
                    <li><a href="versions.php"><i class="fa fa-history"></i> 版本记录</a></li>
                </ul>
            </div>
            <div class="footer-column">
                <h3>联系我们</h3>
                <ul class="footer-links">
                    <li><a href="mailto:support@itops.com"><i class="fa fa-envelope"></i> support@itops.com</a></li>
                    <li><a href="tel:123456789"><i class="fa fa-phone"></i> 123-456-789</a></li>
                </ul>
            </div>
        </div>
        <div class="footer-bottom">
            <p>&copy; <?php echo date('Y'); ?> IT运维知识库 - 版权所有</p>
        </div>
    </div>
</footer>

<script>
    // 移动端菜单控制
    document.getElementById('menuToggle').addEventListener('click', function() {
        document.getElementById('mobileMenu').classList.add('active');
    });
    
    document.getElementById('closeMenu').addEventListener('click', function() {
        document.getElementById('mobileMenu').classList.remove('active');
    });

    // 退出登录功能
    function logout() {
        if (confirm('确定要退出登录吗？')) {
            fetch('api/auth.php?action=logout')
                .then(response => response.json())
                .then(data => {
                    if (data.status === 'success') {
                        window.location.reload();
                    }
                });
        }
    }
</script>
</body>
</html>